home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_166 / autograf / checkup.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  114 lines

  1. /*  checkup.c : program to do a validity check on an autolog data file 
  2.  *
  3.  *    By Joel Swank 8/30/88
  4.  *     Version 1.1
  5.  */
  6.  
  7. /*
  8.    Reads a datafile of gasoline purchases of the following format:
  9.    Individual fields separated by commas.
  10.    * date - character format
  11.    * odometer reading - 999999.9
  12.    * price per gallon - $9.999
  13.    * Total cost of purchase - $9999.99
  14.    * gallons - 999.9
  15.    * place and brand of gas - character
  16.    Execute from cli.
  17.  
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include <ctype.h>
  23.  
  24. char linebuf[100];
  25.  
  26. main(argc,argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.    char *pp, *tp;
  31.    float od, previous, cost, price, atof();
  32.    int yr, previousyr;
  33.    FILE *fp;
  34.    previous = 0;
  35.    previousyr = 0;
  36.    if (argc == 0) exit(0);
  37.    if (argc != 2) 
  38.       {
  39.       fprintf(stderr,"Usage:checkup datafile\n");
  40.       exit(0);
  41.       }
  42.    if ((fp = fopen(argv[1],"r")) == NULL)
  43.       {
  44.       fprintf(stderr,"checkup:%s:File not found\n",argv[1]);
  45.       exit(0);
  46.       }
  47.    while (NULL != fgets(linebuf,100,fp))
  48.       {
  49.  
  50.       pp = linebuf;
  51.  
  52.       while (*(++pp) != ',') badchk(*pp);    /* find end of date */
  53.       pp -= 4;
  54.       yr = atoi(pp);    /* get year */
  55.       if (previousyr)
  56.           {
  57.           if (yr != previousyr && yr != previousyr+1 )
  58.               {
  59.             fprintf(stderr,"Error - year inconsistant with  previous\n");
  60.             fprintf(stderr,"%s\n", linebuf);
  61.             } else previousyr = yr;
  62.         } else previousyr = yr;
  63.  
  64.       pp += 5;
  65.  
  66.       od = atof(pp);    /* get odometer reading */
  67.       if (od < previous)
  68.           {
  69.         fprintf(stderr,"Error - odometer reading less than previous\n");
  70.         fprintf(stderr,"%s\n", linebuf);
  71.         } else previous = od;
  72.  
  73.       while (*(++pp) != ',') badchk(*pp);
  74.       while (*(++pp) != '$') badchk(*pp);    /* get price */
  75.       pp++;
  76.       price = atof(pp);
  77.       if (price > 10.0)
  78.           {
  79.         fprintf(stderr,"Error - price greater than $10.00\n");
  80.         fprintf(stderr,"%s\n", linebuf);
  81.         } 
  82.       if (price <= 0.0)
  83.           {
  84.         fprintf(stderr,"Error - price less than $0.01\n");
  85.         fprintf(stderr,"%s\n", linebuf);
  86.         } 
  87.  
  88.       while (*(++pp) != ',') badchk(*pp);
  89.       while (*(++pp) != '$') badchk(*pp);    /* get cost */
  90.       pp++;
  91.       cost = atof(pp);
  92.       if (cost <= 0.0)
  93.           {
  94.         fprintf(stderr,"Error - cost less than $0.01\n");
  95.         fprintf(stderr,"%s\n", linebuf);
  96.         } 
  97.       }
  98.       fclose(fp);
  99. }
  100.  
  101. /*
  102.  * badchk : check for invalid character and 
  103.  *          terminate if found
  104.  */
  105.  
  106. badchk(ch)
  107. char ch;
  108. {
  109.     if ( isprint(ch) ) return;
  110.     fprintf(stderr,"File Format Error - Aborting.\n");
  111.     fprintf(stderr,"%s\n", linebuf);
  112.     exit(2);
  113. }
  114.